/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月17日         Root.Lu
 */

package com.coocaa.x.framework.utils;

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class UIUtils {
    public static void setViewSize(View view, int width, int height) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        ViewParent parent = view.getParent();
        if (LinearLayout.class.isInstance(parent)) {
            if (width != -1)
                ((LinearLayout.LayoutParams) params).width = width;
            if (height != -1)
                ((LinearLayout.LayoutParams) params).height = height;
        } else if (RelativeLayout.class.isInstance(parent)) {
            if (width != -1)
                ((RelativeLayout.LayoutParams) params).width = width;
            if (height != -1)
                ((RelativeLayout.LayoutParams) params).height = height;
        } else if (FrameLayout.class.isInstance(parent)) {
            if (width != -1)
                ((FrameLayout.LayoutParams) params).width = width;
            if (height != -1)
                ((FrameLayout.LayoutParams) params).height = height;
        }

        view.setLayoutParams(params);
    }

    public static void setViewMargin(View view, int leftMargin, int topMargin, int rightMargin,
                                     int bottomMargin) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        ViewParent parent = view.getParent();
        if (LinearLayout.class.isInstance(parent)) {
            if (leftMargin != -1)
                ((LinearLayout.LayoutParams) params).leftMargin = leftMargin;
            if (topMargin != -1)
                ((LinearLayout.LayoutParams) params).topMargin = topMargin;
            if (rightMargin != -1)
                ((LinearLayout.LayoutParams) params).rightMargin = rightMargin;
            if (bottomMargin != -1)
                ((LinearLayout.LayoutParams) params).bottomMargin = bottomMargin;
        } else if (RelativeLayout.class.isInstance(parent)) {
            if (leftMargin != -1)
                ((RelativeLayout.LayoutParams) params).leftMargin = leftMargin;
            if (topMargin != -1)
                ((RelativeLayout.LayoutParams) params).topMargin = topMargin;
            if (rightMargin != -1)
                ((RelativeLayout.LayoutParams) params).rightMargin = rightMargin;
            if (bottomMargin != -1)
                ((RelativeLayout.LayoutParams) params).bottomMargin = bottomMargin;
        } else if (FrameLayout.class.isInstance(parent)) {
            if (leftMargin != -1)
                ((FrameLayout.LayoutParams) params).leftMargin = leftMargin;
            if (topMargin != -1)
                ((FrameLayout.LayoutParams) params).topMargin = topMargin;
            if (rightMargin != -1)
                ((FrameLayout.LayoutParams) params).rightMargin = rightMargin;
            if (bottomMargin != -1)
                ((FrameLayout.LayoutParams) params).bottomMargin = bottomMargin;
        }

        view.setLayoutParams(params);
    }

    public static void setViewPosition(View view, int x, int y) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        ViewParent parent = view.getParent();
        if (LinearLayout.class.isInstance(parent)) {
            if (x != -1)
                ((LinearLayout.LayoutParams) params).leftMargin = x;
            if (y != -1)
                ((LinearLayout.LayoutParams) params).topMargin = y;
        } else if (RelativeLayout.class.isInstance(parent)) {
            if (x != -1)
                ((RelativeLayout.LayoutParams) params).leftMargin = x;
            if (y != -1)
                ((RelativeLayout.LayoutParams) params).topMargin = y;
        } else if (FrameLayout.class.isInstance(parent)) {
            if (x != -1)
                ((FrameLayout.LayoutParams) params).leftMargin = x;
            if (y != -1)
                ((FrameLayout.LayoutParams) params).topMargin = y;
        }

        view.setLayoutParams(params);
    }
}
